package edu.gatech.cs2340.group7.wheresmystuffapp.views;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import edu.gatech.cs2340.group7.wheresmystuffapp.R;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.CustomDate;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.FilterResult;
import edu.gatech.cs2340.group7.wheresmystuffapp.presenters.ReportedItemsPresenter;

public class ReportedItemsView extends Activity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {

	private TextView ListLostItems, ListFoundItems;
	private CustomDate dateEntered;
	private Button backButton, FilterButton, ResetButton;
	private CheckBox filterByDate, filterByLost, filterByFound, filterByMisc,
			filterByHeirloom, filterByKeepsake;
	private EditText DateFilter;
	private ReportedItemsPresenter p;
	private FilterResult result;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_show_reported_items);
		init();
		p.showItems(result);

	}

	/**
	 * Initializes all of the views in the XML layout file, and sets the listeners for the buttons
	 */
	private void init() {
		ListLostItems = (TextView) findViewById(R.id.ListOfLostItems);
		ListFoundItems = (TextView) findViewById(R.id.ListOfFoundItems);
		backButton = (Button) findViewById(R.id.Reported_Items_Back_Button);
		DateFilter = (EditText) findViewById(R.id.et_date_filter);
		FilterButton = (Button) findViewById(R.id.filterItemsButton);
		ResetButton = (Button) findViewById(R.id.reset_Filter_Button);

		filterByDate = (CheckBox) findViewById(R.id.chkDate);
		filterByDate.setOnCheckedChangeListener(this);

		filterByLost = (CheckBox) findViewById(R.id.radioFilterCategoryLostItems);
		filterByLost.setOnCheckedChangeListener(this);

		filterByFound = (CheckBox) findViewById(R.id.radioFilterCategoryFoundItems);
		filterByFound.setOnCheckedChangeListener(this);

		filterByMisc = (CheckBox) findViewById(R.id.radioFilterCategoryMisc);
		filterByMisc.setOnCheckedChangeListener(this);

		filterByKeepsake = (CheckBox) findViewById(R.id.radioFilterCategoryKeepsakes);
		filterByKeepsake.setOnCheckedChangeListener(this);

		filterByHeirloom = (CheckBox) findViewById(R.id.radioFilterCategoryHeirlooms);
		filterByHeirloom.setOnCheckedChangeListener(this);

		result = FilterResult.LOSTFOUND;

		backButton.setOnClickListener(this);
		FilterButton.setOnClickListener(this);
		ResetButton.setOnClickListener(this);
		p = new ReportedItemsPresenter(this);

	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.Reported_Items_Back_Button:
			Intent intent = new Intent(this, UserMainMenuView.class);
			p.backToMainMenu(intent);
			break;
		case R.id.filterItemsButton:
			if (filterByDate.isChecked()) {
				String date = DateFilter.getText().toString();
				dateEntered = new CustomDate(date);
			}
			// ListLostItems.setText("Filtering Items by " + result.toString());
			p.showItems(result);
			break;
		case R.id.reset_Filter_Button:
			result = FilterResult.LOSTFOUND;
			p.showItems(result);
			break;
		}
	}

	public void displayItems(String listLostItems, String listFoundItems) {
		if (listLostItems == null || listLostItems.length() == 0)
			listLostItems = "None Reported";
		if (listFoundItems == null || listFoundItems.length() == 0)
			listFoundItems = "None Reported";
		ListLostItems.setText(listLostItems);
		ListFoundItems.setText(listFoundItems);

	}

	public CustomDate getDateEntered() {
		return dateEntered;
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if (filterByDate.isChecked()) {
			result = FilterResult.DATE;
			System.out.println("Filtering by " + result.toString());
			// ListLostItems.setText("Filtering by " + result.toString());
		}
		if (filterByLost.isChecked()) {
			result = FilterResult.LOST;
			System.out.println("Filtering by " + result.toString());
			// ListLostItems.setText("Filtering by " + result.toString());
		}

		if (filterByFound.isChecked()) {
			result = FilterResult.FOUND;
			System.out.println("Filtering by " + result.toString());
			// ListLostItems.setText("Filtering by " + result.toString());
		}

		if (filterByKeepsake.isChecked()) {
			result = FilterResult.KEEPSAKE;
			System.out.println("Filtering by " + result.toString());
			// ListLostItems.setText("Filtering by " + result.toString());
		}

		if (filterByHeirloom.isChecked()) {
			result = FilterResult.HEIRLOOM;
			System.out.println("Filtering by " + result.toString());
			// ListLostItems.setText("Filtering by " + result.toString());
		}

		if (filterByMisc.isChecked()) {
			result = FilterResult.MISC;
			System.out.println("Filtering by " + result.toString());
			// ListLostItems.setText("Filtering by " + result.toString());
		}
	}

}
